package com.huterox.todoscheduler.core.execute;

import com.huterox.todoscheduler.core.enumType.TodoListStateType;
import com.huterox.todoscheduler.core.execute.proxy.TodoItemExBean;
import com.huterox.todoscheduler.core.execute.proxy.TodoListExBean;
import com.huterox.todoscheduler.core.global.TodoListTemplateMap;
import com.huterox.todoscheduler.core.wapper.TodoItemMethodWrapper;
import com.huterox.todoscheduler.core.wapper.TodoListWrapper;
import com.huterox.todoscheduler.exception.TodoListException;
import com.huterox.todoscheduler.handler.TodoListBeforeCreateHandler;

import java.io.Serializable;
import java.util.Map;

/**
 * 创建真正的任务执行清单对象，
 * 通过这个ToDoListBean的信息来进行创建
 * */
public class TodoListFactory implements Serializable {

    /**
     *   创建可以执行的清单对象，然后对执行器就会执行这个对象，执行的是TodoListExBean
     *   如果返回结果为null,说明连这个清单都没有
     * */

    public static TodoListExBean getInstance(String listName) {
        TodoListExBean todoListExBean = new TodoListExBean();

        if(!TodoListTemplateMap.getInstance().containKey(listName)){
            new TodoListException("未找到任务清单失败："+listName,-1).printStackTrace();
            todoListExBean.setTodoListStateType(TodoListStateType.CreatFailed);
            return null;
        }
        //先获取到清单的模板
        TodoListWrapper todoListTemplate = TodoListTemplateMap.getInstance().get(listName);

        todoListExBean.setTodoListName(todoListTemplate.getTodoListName());
        todoListExBean.setTodoListAfterHandler(todoListTemplate.getTodoListAfterHandler());
        todoListExBean.setTodoListCreateCheckHandler(todoListTemplate.getTodoListCreateCheckHandler());
        todoListExBean.setTodoListErrorHandler(todoListTemplate.getTodoListErrorHandler());

        //执行创建前的方法
        if(todoListExBean.getTodoListCreateCheckHandler()!=null){
            try {
                TodoListBeforeCreateHandler todoListCreateCheckHandler = todoListExBean.getTodoListCreateCheckHandler();
                //先执行这个获取ClsID的方法
                try {
                    String s = todoListCreateCheckHandler.setCIds();
                    if(!("".equals(s))){
                        todoListExBean.setClsId(s);
                    }else {
                        todoListExBean.setClsId("");
                    }
                }catch (Exception e){
                    e.printStackTrace();
                    todoListExBean.setTodoListStateType(TodoListStateType.CreatFailed);
                    return todoListExBean;
                }
            }catch (Exception exception){
                exception.printStackTrace();
                todoListExBean.setTodoListStateType(TodoListStateType.CreatFailed);
                return todoListExBean;
            }
        }

        //遍历模板，然后将可执行的清单项放在我们的这个TodoListBeanEx的执行项目里面

        Map<Integer, TodoItemExBean> sortedMapEx = todoListExBean.getSortedMap();

        Map<Integer, TodoItemMethodWrapper> sortedMap = todoListTemplate.getSortedMap();
        for(Map.Entry<Integer, TodoItemMethodWrapper> sortedMapEntry:sortedMap.entrySet()){
            TodoItemMethodWrapper mapEntryValue = sortedMapEntry.getValue();
            Integer key = sortedMapEntry.getKey();
            TodoItemExBean instance = TodoItemFactory.getInstance(mapEntryValue,todoListExBean);
            if(instance==null){
                //如果这个清单项创建失败，那也是失败
                return null;
            }
            sortedMapEx.put(key,instance);

        }
        //此时创建完毕，但是这里每一个方法如何执行，也就是清单项是调度器考虑的事情了
        return todoListExBean;
    }
}
